package com.itc.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bpm.bean.BpmProcessCategory;
import com.itc.bpm.dto.BpmProcessCategoryDto;
import com.itc.bpm.mapper.BpmProcessCategoryMapper;
import com.itc.bpm.service.BpmProcessCategoryService;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BpmProcessCategoryServiceImpl extends ServiceImpl<BpmProcessCategoryMapper, BpmProcessCategory> implements BpmProcessCategoryService {
    @Override
    public R addCategory(BpmProcessCategory bpmModel) {
        if(bpmModel.getId()==null){ // 新增
            if(bpmModel.getParentId()==null){
                bpmModel.setParentId(0L);
            }
            bpmModel.setOrderNo(null);
            BpmProcessCategory bpmProcessCategory = this.getOne(new LambdaQueryWrapper<BpmProcessCategory>().eq(BpmProcessCategory::getParentId,bpmModel.getParentId()).orderByDesc(BpmProcessCategory::getCreateTime).last("limit 1"));
            if(bpmProcessCategory==null && bpmModel.getParentId()==0L){
                bpmModel.setCode("10");
                bpmModel.setOrderNo(10);
            } else if(bpmProcessCategory==null && bpmModel.getParentId()!=0L){
                BpmProcessCategory bpmProcessCategory1 = this.getById(bpmModel.getParentId());
                bpmModel.setCode(bpmProcessCategory1.getCode()+"01");
                bpmModel.setOrderNo(1);
            }else if(bpmProcessCategory!=null && bpmModel.getParentId()==0L){
                bpmModel.setOrderNo(bpmProcessCategory.getOrderNo()+1);
                bpmModel.setCode(bpmModel.getOrderNo()+"");
            }else if(bpmProcessCategory!=null && bpmModel.getParentId()!=0L){
                BpmProcessCategory bpmProcessCategory1 = this.getById(bpmModel.getParentId());
                bpmModel.setOrderNo(bpmProcessCategory.getOrderNo()+1);
                if(bpmModel.getOrderNo()<10){
                    bpmModel.setCode(bpmProcessCategory1.getCode()+"0"+bpmModel.getOrderNo());
                }else{
                    bpmModel.setCode(bpmProcessCategory1.getCode()+""+bpmModel.getOrderNo());
                }
            }
            this.save(bpmModel);
        }else{
            BpmProcessCategory category = this.getById(bpmModel.getId());
            if(category==null){
                return R.fail("分类不存在，请重新修改");
            }
            if(bpmModel.getParentId()!=null && !category.getParentId().equals(bpmModel.getParentId())){
                return R.fail("不能修改父节点");
            }
            this.updateById(bpmModel);
        }
        return R.ok();
    }

    @Override
    public R<List<BpmProcessCategoryDto>> queryAll() {
        List<BpmProcessCategoryDto> data = new ArrayList<>();   // 存放输出的数据
        // 1.获取全部数据
        List<BpmProcessCategory> all = this.list(new LambdaQueryWrapper<BpmProcessCategory>().eq(BpmProcessCategory::getDelFlag,0).orderByAsc(BpmProcessCategory::getOrderNo));
        if(all!=null && all.size()>0){
            List<BpmProcessCategoryDto> siteVoList = all.stream().map(BpmProcessCategory::getVoByEntity).collect(Collectors.toList());
            siteVoList.forEach(e ->{
                // 查询目录下有无子目录
                List<BpmProcessCategoryDto> childrenList = getChildrenList(e.getId(), siteVoList);
                e.setChildren(StringUtils.isNotEmpty(childrenList) ? childrenList : null);
            });
            // 3.只需要向前端输出pId=0的顶点目录
            List<BpmProcessCategoryDto> parentSiteList = siteVoList.stream().filter(e -> e.getParentId().equals(0L)).collect(Collectors.toList());
            data.addAll(parentSiteList);
        }
        return R.ok(data);
    }

    // 获取当前目录的全部子集合
    private static List<BpmProcessCategoryDto> getChildrenList(Long id, List<BpmProcessCategoryDto> list) {
        // 遍历所有数据，过滤出需要的子集合数据
        return list.stream().filter(t -> t.getParentId().equals(id)).collect(Collectors.toList());
    }

}
